from datetime import datetime

from libqtile.lazy import lazy

# Toggles the clipboard manager descriptor between "CE" and "CD"

@lazy.function
def toggle_clipboard(qtile, clipboard):
    if clipboard[0] == False :
        clipboard[0] = True
        qtile.widgets_map["clipboard"].update(' CE ')
        qtile.cmd_spawn("clipctl enable")
        qtile.cmd_spawn('dunstify -h string:x-dunst-stack-tag:clipboard_state "Qtile" "Enabled clipboard recording"')
    else :
        clipboard[0] = False
        qtile.widgets_map["clipboard"].update(' CD ')
        qtile.cmd_spawn("clipctl disable")
        qtile.cmd_spawn('dunstify -h string:x-dunst-stack-tag:clipboard_state "Qtile" "Disabled clipboard recording"')


@lazy.function
def take_screenshot(qtile, fullscreen, select, directory, dateformat, fileformat, bzipcompression):
    now = datetime.now()
    screenshot_name=directory+"/"+now.strftime(dateformat)+"."+fileformat
    command="screenshot '" + screenshot_name + "'"
    if fullscreen:
        command = command + " True False"
    elif select:
        command = command + " False True"
    else:
        command = command + " False False"
    if bzipcompression:
        command = command + " True"
    else:
        command = command + " False"
    qtile.cmd_spawn(command)

@lazy.function
def record(qtile, directory, format, widget_color):
    now = datetime.now()
    recordname=directory + "/" + now.strftime(format)+".mkv"
    qtile.cmd_spawn("record '" + recordname + "'")
    if qtile.widgets_map["record"].cmd_get() == '':
        qtile.widgets_map["record"].update(' <span foreground="' + widget_color + '">REC</span> ')
    else:
        qtile.widgets_map["record"].update('')

@lazy.function
def change_pulse_volume(qtile, amount):
    qtile.cmd_spawn('pactl set-sink-volume @DEFAULT_SINK@ ' + amount)
    qtile.cmd_spawn("volume-notification Qtile")

@lazy.function
def toggle_pulse_mute(qtile):
    qtile.cmd_spawn('pactl set-sink-mute @DEFAULT_SINK@ toggle')
    qtile.cmd_spawn("mute-notification Qtile")

# Rotates between the input methods specified in the inputs array

@lazy.function
def change_input(qtile, inputs, current):
    current[0]=(current[0]+1)%len(inputs)
    qtile.cmd_spawn('ibus engine ' + inputs[current[0]])
    qtile.cmd_spawn("ibus-notification Qtile")
    

@lazy.function
def my_shutdown(qtile, target):
    target_dict = dict (
            reboot="loginctl reboot",
            shutdown="loginctl poweroff",
            logout="loginctl kill-session self",
            lock="dm-tool lock",
            change_user="dm-tool switch-to-greeter",
    )
    qtile.cmd_spawn(target_dict[target])

@lazy.function
def toggle_bars(qtile):
    qtile.cmd_hide_show_bar()

